/**
 * 专题配置
 */
var serverAction = (function(){
	//服务资源面板
	var serverResourcePanelInit=function(){
		$('#serverResource').panel({  
			  width:260,  
			  height:500,
			  title: '服务资源',
		});
		$('#serverResource').closest(".panel").css({"display":"inline-block","margin-top":"10px","margin-left":"10px"});
	};
	//功能模块面板
	var moduleSourcePanelInit=function(){
		$('#actionResource').panel({  
			  width:260,  
			  height:500,
			  title:'功能模块'
		});
		$('#actionResource').closest(".panel").css({"display":"inline-block","margin-top":"10px"});
	};
	var fieldConfigInit=function(){
		$('#fieldConfig').panel({  
			  width:300,  
			  height:500,
			  title:'参数配置'
		});
		$('#fieldConfig').closest(".panel").css({"display":"inline-block","margin-top":"10px"});
		$("input[name='JCFM']").on("click",function(){
			if($(this).val()=="False"){
				$(".GJQFSelect").attr({"disabled":true});
				$(".GJQFVSelect").attr({"disabled":true});
			}else{
				$(".GJQFSelect").attr({"disabled":false});
				$(".GJQFVSelect").attr({"disabled":false});
			}
		})
		$(".GJQFSelect").change(function(){
			$(".GJQFVSelect #defaultOp").siblings().remove();
			var GJQFField=$(".GJQFSelect option:selected").val();
			$.getJSON(currentLayerInfo.url+'/exts/SDDCMapSOE/GetUniqueValue?LayerIndex='+currentLayerInfo.index+'&FieldName='+GJQFField+"&f=json",function(result){
				var UniqueryArray=result.UniqueValue;
				$.each(UniqueryArray,function(i,item){
				   $("<option value="+item+">"+item+"</option>").insertBefore($(".GJQFVSelect #defaultOp"));
				})
			})
		})
		$("input.submit").click(function(){
			var fieldExpress={};
			debugger
			fieldExpress.AreaField=$(".AreaFieldSelect option:selected").val();
			fieldExpress.GJQFField=$(".GJQFSelect option:selected").val();
			fieldExpress.GJQFValue=$(".GJQFVSelect option:selected").val();
			$.ajax({
				type:"POST",
				url:basePath+'/ServerResourceController/addActionServerConfig',
				data:{
					  moduleId:$("input[name='ModuleId']").val(),
					  layerId:$("input[name='LayerId']").val(),
					  layerParam:$("input.LayerParam").val(),
					  fieldsExpress:"AreaField:"+fieldExpress.AreaField+";GJQFField:"+fieldExpress.GJQFField+";GJQFValue:"+fieldExpress.GJQFValue
					  },
			    success:function(msg){
			    	var obj=jQuery.parseJSON(msg);
			    	$.messager.show({title:'提示',msg:obj.msg})
			    }
			})
		})
	};
	//服务资源树数据
	
	var currentLayerInfo=null;
	var serverResourceTreeInit=function(){
		$("#serverResourceTree").tree({
		    url:basePath+'/ServerResourceController/tree',
			lines:false,//默认是false
  			checkbox:false,//默认是false   
  			onlyLeafCheck:true,//默认是false
  			cascadeCheck:false,//默认是true
  			parentField:'pid',
  			onSelect: function (node) {
  				if(node.children){
  					return false
  				}else{
  					$(".GJQFSelect #defaultOp").siblings().remove();
  					$(".AreaFieldSelect #defaultOp").siblings().remove();
  	                $.getJSON(basePath+'/ServerResourceController/getLayerInfoByIds',{id:node.id,pid:node.pid},function(results){
  	                	$("input[name='LayerId']").val(results.layId);
  	            		$.getJSON(results.dUrl+'/exts/SDDCMapSOE/GetLayerInfo?LayerIndex='+results.layIndex+"&f=json",function(fields){
  	            			currentLayerInfo={url:results.dUrl,index:results.layIndex,fields:fields.fields};
  	            			$.each(currentLayerInfo.fields,function(i,item){
  	            				$("<option value="+item.Name+">"+item.AliasName+"</option>").insertBefore($(".GJQFSelect #defaultOp"));
  	            				$("<option value="+item.Name+">"+item.AliasName+"</option>").insertBefore($(".AreaFieldSelect #defaultOp"));
  	            			})})})}
  				 },
            onLoadSuccess: function (node, data) {
                $(this).find('span.tree-checkbox').unbind().click(function () {
                    $('#serverResourceTree').tree('select', $(this).parent());
                    return false;
                });
            }
		});
	};
	//功能模块树数据
	var moduleResourceTreeInit=function(){
		$("#actionResourceTree").tree({
		    url:basePath+'/ServerResourceController/modelTree',
			lines:false,//默认是false
  			checkbox:false,//默认是false   
  			onlyLeafCheck:true,//默认是false
  			cascadeCheck:false,//默认是true
  			parentField:'pid',
            onLoadSuccess: function (node, data) {
                $(this).find('span.tree-checkbox').unbind().click(function () {
                    $('#actionResourceTree').tree('select', $(this).parent());
                    return false;
                });
            },
            onSelect: function (node) {
            	var node=node;
            	console.log(basePath);
            	if(node.children!==undefined){
            		return false;
            	}
            	$(".LayerParam").val(node.text);
            	$("input[name='ModuleId']").val(node.pid);
            	$.ajax({
            		url:basePath+'/ServerResourceController/getModuleServerConfigByModuleIdAndLayerParam',
            		data:
            		    {ModuleId:node.pid,layerParam:node.text},
            	    success:function(result){
            	    	debugger
            	    	var obj=jQuery.parseJSON(result);
            	    	var layerNodeID=obj.result.layerId;
            	    	var node = $("#serverResourceTree").tree('find',layerNodeID);
			        	$("#serverResourceTree").tree('select',node.target);
            	    	if(obj.success==true){
            	    		debugger
            	    		var configobj=obj.result.fields.split(";");
                	    	var JCFM=configobj[1].split(":")[1]=="null"?"否":"是";
                	    	var GJQFF=configobj[1].split(":")[1]=="null"?"缺省":configobj[1].split(":")[1];
                	    	var GJQFV=configobj[2].split(":")[1]=="null"?"缺省":configobj[2].split(":")[1]
                	    	var AREA=configobj[0].split(":")[1]=="null"?"缺省":configobj[0].split(":")[1];
                	    	var LAYERPARAM=obj.result.layerParam;
                	    	$(".dataintable .SFSJ").text(JCFM);
                	    	$(".dataintable .GJQF").text(GJQFF);
                	    	$(".dataintable .QFBZ").text(GJQFV);
                	    	$(".dataintable .ZWCS").text(LAYERPARAM);
                	    	$(".dataintable .MJZD").text(AREA);
            	    	}else{
            	    		$(".dataintable .SFSJ").text("暂无");
                	    	$(".dataintable .GJQF").text("暂无");
                	    	$(".dataintable .QFBZ").text("暂无");
                	    	$(".dataintable .ZWCS").text("暂无");
                	    	$(".dataintable .MJZD").text("暂无");
            	    	}
            	    	$.messager.show({title:'提示',msg:obj.msg})
            	    }
            	
            	})
               },
		});
		
	};
	
	
	
	return {
		init:function(){
			serverResourcePanelInit();
			serverResourceTreeInit();
			
			moduleSourcePanelInit();
			moduleResourceTreeInit();
			
			fieldConfigInit();
		}
	}
})();